<template>
	<view class="layout">

		<view class="header-top">

		</view>
		<view class="header-top acea-row row-between-wrapper row-middle">
			<view>
				<text class="iconfont icon-fanhui2 px-20" style="color: #fff;" @tap="returns"></text>
			</view>
			<view class="header-top-tittle">我的发布-二手</view>
			<view>
				<img src="../static/images/add.png" style='width: 40rpx; height: 40rpx;margin-top: 15rpx;' alt="">
			</view>
		</view>
		<view class="acea-row row-middle row-between nav-bg">
			<scroll-view scroll-x="true" style="white-space: nowrap; display: flex;" scroll-with-animation
				:scroll-left="tabLeft" show-scrollbar="true">
				<view class="item" :class="index===categoryOn?'item-on':''" v-for="(item,index) in categoryList"
					:style='"width:"+isWidth+"px"' :key="index" @click="categoryNavClick(item,index)">
					<view class="acea-row row-middle row-center img">
						<image :src="item.icon" mode=""></image>
					</view>
					<view class="name">
						{{item.name}}
					</view>
				</view>
			</scroll-view>
		</view>

		<view class="acea-row row-middle type-bg">
			<view class="type" :class="typeOn==index?'type-on':''" v-for="(item,index) in types" :key="index"
				@click="typeNavClick(item,index)">
				{{item.name}}
			</view>
		</view>
		<view class="scroll">
			<scroll-view :scroll-top="scrollTopList" scroll-y="true" class="scroll-Y" @scrolltolower="lower">
				<view v-for="(item,index) in unusedList" :key="index" style="margin-bottom: 20rpx;">
					<hand :item="item" :typeOn="typeOn" :categoryOn ="categoryOn" @to-topping="toTopping" @updateItem="((k,v)=>$set(unusedList[index],k,v))" @toVip="toVip" @setMeth="setMeth"  @visitor="toVisitor" />
				</view>
				<view v-if="unusedList.length<1" style="text-align: center;margin-top:  35rpx;">
					暂无更多数据
				</view>
			</scroll-view>
		</view>
		<topping :show="linkShow" :link-id="linkId" :types="linkTypes" @close="linkShow=false" @opear="toOpear" />
		<setting ref="setting" :show="setShow" @close="setShow=false" :item="settingItem" @opear="toOpear" @toChange="settingchange" />
	</view>
</template>

<script>
	import hand from '../components/hand.vue'
	import {
		getCategoryFirst
	} from '@/api/api.js'
		import {
			getclassificationListApi
		} from '@/api/brand/index.js'
	import {
		unusedListOnline
	} from '@/api/unused.js'
	import {
		showLoading
	} from '@/filters/commFilter.js'
	import topping from '../components/topping.vue'
	import setting from '../components/setting.vue'

	export default {
		components: {
			hand,
			topping,
			setting
		},
		created() {
			var that = this
			// 获取设备宽度
			uni.getSystemInfo({
				success(e) {
					that.isWidth = e.windowWidth / 6
				}
			})
		},
		data() {
			return {
				isWidth: 58,
				types: [{
					name: '出售',
					type: 0
				}, {
					name: '求购',
					type: 1
				}],
				categoryOn: 0,
				typeOn: 0,
				page: 1,
				tabLeft: 0,
				categoryList: [],
				scrollTopList: 0,
				unusedList: [],
				settingItem: {},
				linkId:0,
				linkTypes:'',
				linkShow: false,
				setShow: false,
			}
		},
		mounted() {
			this.getCategoryListMeth();
		},
		methods: {
			toOpear(message) {
				this.$util.Tips({
					title: message
				})
				this.page = 1;
				this.getUnsedList();
			},
			setMeth(data) {
				this.settingItem = data;
				console.log("setData", data)
				this.setShow = true
			},
			toVip(data) {
				this.settingItem = data;
				console.log("this.settingItem", this.settingItem)
				let that = this;
				let ti = data.isVip && data.isVip == 1 ? '取消' : '开启';
				let st = data.isVip && data.isVip == 1 ? 0 : 1;
				uni.showModal({
					title: '提示',
					content: '是否' + ti + '信息VIP',
					success(res) {
						if (res.confirm) {
							that.$refs.setting.settingVip(st)
						}
					}
				})
			},
			toTopping(linkId, linkType) {
				this.linkId = linkId;
				this.linkTypes = linkType;
				this.linkShow = true;
			},
			settingchange(k, v) {
				this.settingItem[k] = v;
				// this.$set(this.settingItem,k,v)
				this.unusedList.forEach(item => {
					if (item.id && item.id == this.settingItem.id) {
						console.log('sdfsfsf')
						item[k] = this.settingItem[k]
					}
				})
			},
			toVisitor(data) {
				uni.navigateTo({
					url: "/pages/aboutUsers/aboutUsers/visitor?mind=1&data=" + JSON.stringify(data)
				})
			},
			typeNavClick(item, index) {
				// let title = '发布-'+this.categoryList[this.categoryOn].name+item.name
				// let title = '发布-'+item.name
				// uni.setNavigationBarTitle({
				// 	title:title
				// })
				this.typeOn = index
				this.page = 1;
				this.getUnsedList();
			},
			async getUnsedList() {
				showLoading()
				if(this.page == 1){
					this.unusedList = []
				}
				try {
					let res = await unusedListOnline({
						page: this.page,
						status: this.types[this.typeOn].type,
						goodCate : this.categoryList[this.categoryOn].id
					});
					uni.hideLoading()
					this.unusedList = [...this.unusedList,...res.data.list];
					this.unusedList = this.unusedList ? this.unusedList.map(item => {
						item.vistype = 2;
						return item;
					}) : [];
					this.getList()

				} catch (e) {

				} finally {

				}
			},
			getCategoryListMeth() {
				getclassificationListApi().then(res => {
					console.log('getclassificationListApi', res)
					this.categoryList = res.data;
					this.getUnsedList();
				})
			},
			//刷新
			onRefresh() {
				// this.categoryOn = 0;
				// this.typeOn = 0;
				this.page = 1;
				this.dataList = [];
				this.getList()
			},
			lower() {
				this.page++;
				this.getUnsedList()
			},
			returns: function() {
				uni.navigateBack();
			},
			// 导航栏点击
			categoryNavClick(item, index) {
				// let title = '发布-'+item.name+this.types[this.typeOn].name
				// uni.setNavigationBarTitle({
				// 	title:title
				// })
				if (this.categoryList.length > 6) {
					var tempIndex = index - 2;
					tempIndex = tempIndex <= 0 ? 0 : tempIndex;
					this.tabLeft = (index - 2) * this.isWidth //设置下划线位置
				}
				this.categoryOn = index //设置导航点击了哪一个
				this.isLeft = index * this.isWidth //设置下划线位置
				this.page = 1;
				this.getUnsedList();
			},
		}
	}
</script>

<style lang="scss">
	.layout {
		display: flex;
		flex-direction: column;
		height: 100vh;

		.scroll {
			overflow-y: auto;
			padding-bottom: 35rpx;
			flex: 1;

			.scroll-Y {
				height: 100%;
			}
		}

	}

	.header-top {
		height: 88rpx;
		background: rgba(238, 33, 45, 1);
		padding: 0rpx 30rpx;

		.header-top-tittle {
			font-size: 36rpx;
			color: rgba(255, 255, 255, 1);
		}
	}

	.nav-bg {
		background: #ffffff;
		padding: 18rpx 30rpx 36rpx;

		.item~.item {
			// margin-left: 48rpx;
		}

		.item {
			display: inline-block;
			white-space: nowrap;

			.img {
				width: 70rpx;
				height: 70rpx;
				border-radius: 50%;
				background: #ffffff;
				margin: 0 auto;

				image {
					width: 100%;
					height: 100%;
				}
			}

			.name {
				// width: 70rpx;
				font-size: 26rpx;
				font-weight: 400;
				line-height: 40rpx;
				color: rgba(0, 0, 0, 1);
				text-align: center;
			}
		}

		.item-on {
			.img {
				background: rgba(247, 247, 247, 1);
			}

			.name {
				color: rgba(239, 39, 50, 1);
			}
		}
	}

	.type-bg {
		padding: 24rpx 30rpx;
		background: rgba(247, 248, 250, 1);

		.type~.type {
			margin-left: 18rpx;
		}

		.type {
			padding: 6rpx 24rpx;
			border-radius: 10rpx;
			border: 1px solid rgba(229, 229, 229, 1);
			font-size: 32rpx;
			font-weight: 400;
			color: rgba(128, 128, 128, 1);
		}

		.type-on {
			border: 1px solid rgba(238, 33, 45, 1);
			background: rgba(238, 33, 45, 1);
			color: #ffffff;

		}
	}
</style>